home *** CD-ROM | disk | FTP | other *** search
/ Programming Windows 95 with MFC / Programming Windows 95 with MFC (Microsoft Programming Series)(097-0001465)(1996).iso / CODE / Chap06 / PropDemo / PropDemo.cpp next >
Encoding:
C/C++ Source or Header  |  1996-04-05  |  7.8 KB  |  303 lines

  1. //***********************************************************************
  2. //
  3. //  PropDemo.cpp
  4. //
  5. //***********************************************************************
  6.  
  7. #include <afxwin.h>
  8. #include <afxdlgs.h>
  9. #include "Resource.h"
  10. #include "PropDemo.h"
  11.  
  12. CMyApp myApp;
  13.  
  14. /////////////////////////////////////////////////////////////////////////
  15. // CMyApp member functions
  16.  
  17. BOOL CMyApp::InitInstance ()
  18. {
  19.     m_pMainWnd = new CMainWindow;
  20.     m_pMainWnd->ShowWindow (m_nCmdShow);
  21.     m_pMainWnd->UpdateWindow ();
  22.     return TRUE;
  23. }
  24.  
  25. /////////////////////////////////////////////////////////////////////////
  26. // CMainWindow message map and member functions
  27.  
  28. BEGIN_MESSAGE_MAP (CMainWindow, CFrameWnd)
  29.     ON_WM_ERASEBKGND ()
  30.     ON_WM_PAINT ()
  31.     ON_COMMAND (IDM_OPTIONS_PROPERTIES, OnOptionsProperties)
  32.     ON_COMMAND (IDM_OPTIONS_EXIT, OnOptionsExit)
  33.     ON_COMMAND (IDM_OPTIONS_ABOUT, OnOptionsAbout)
  34. END_MESSAGE_MAP ()
  35.  
  36. CMainWindow::CMainWindow ()
  37. {
  38.     m_nFillType = 1;
  39.     m_nFillColor = 2;
  40.     m_text = "Hello, MFC";
  41.     m_nHeight = 72;
  42.     m_bBold = TRUE;
  43.     m_bItalic = TRUE;
  44.  
  45.     Create (NULL, "PropDemo", WS_OVERLAPPEDWINDOW, rectDefault, NULL,
  46.         MAKEINTRESOURCE (IDR_MAINFRAME));
  47. }
  48.  
  49. BOOL CMainWindow::OnEraseBkgnd (CDC* pDC)
  50. {
  51.     CRect rect;
  52.     GetClientRect (&rect);
  53.  
  54.     m_nFillType == 1 ? DoGradientFill (pDC, &rect) :
  55.         DoSolidFill (pDC, &rect);
  56.     return TRUE;
  57. }
  58.  
  59. void CMainWindow::OnPaint ()
  60. {
  61.     CRect rect;
  62.     GetClientRect (&rect);
  63.  
  64.     CPaintDC dc (this);
  65.     DoDrawText (&dc, &rect);
  66. }
  67.  
  68. void CMainWindow::OnOptionsProperties ()
  69. {
  70.     CMyPropertySheet ps (this);
  71.  
  72.     ps.m_bkgndPage.m_nFillType = m_nFillType;
  73.     ps.m_bkgndPage.m_nFillColor = m_nFillColor;
  74.     ps.m_textPage.m_text = m_text;
  75.     ps.m_textPage.m_nHeight = m_nHeight;
  76.     ps.m_textPage.m_bBold = m_bBold;
  77.     ps.m_textPage.m_bItalic = m_bItalic;
  78.  
  79.     if (ps.DoModal () == IDOK) {
  80.         m_nFillType = ps.m_bkgndPage.m_nFillType;
  81.         m_nFillColor = ps.m_bkgndPage.m_nFillColor;
  82.         m_text = ps.m_textPage.m_text;
  83.         m_nHeight = ps.m_textPage.m_nHeight;
  84.         m_bBold = ps.m_textPage.m_bBold;
  85.         m_bItalic = ps.m_textPage.m_bItalic;
  86.  
  87.         Invalidate ();
  88.     }
  89. }
  90.  
  91. void CMainWindow::OnOptionsExit ()
  92. {
  93.     SendMessage (WM_CLOSE, 0, 0);
  94. }
  95.  
  96. void CMainWindow::OnOptionsAbout ()
  97. {
  98.     CAboutDialog dlg (this);
  99.     dlg.DoModal ();
  100. }
  101.  
  102. void CMainWindow::ApplyNow (CMyPropertySheet* pPs)
  103. {
  104.     m_nFillType = pPs->m_bkgndPage.m_nFillType;
  105.     m_nFillColor = pPs->m_bkgndPage.m_nFillColor;
  106.     m_text = pPs->m_textPage.m_text;
  107.     m_nHeight = pPs->m_textPage.m_nHeight;
  108.     m_bBold = pPs->m_textPage.m_bBold;
  109.     m_bItalic = pPs->m_textPage.m_bItalic;
  110.  
  111.     Invalidate ();
  112. }
  113.  
  114. void CMainWindow::DoSolidFill (CDC* pDC, CRect* pRect)
  115. {
  116.     static COLORREF crColor[5] = {
  117.         RGB (255,   0,   0),
  118.         RGB (  0, 255,   0),
  119.         RGB (  0,   0, 255),
  120.         RGB (255,   0, 255),
  121.         RGB (  0, 255, 255),
  122.     };
  123.  
  124.     CBrush brush (crColor[m_nFillColor]);        
  125.     pDC->FillRect (pRect, &brush);
  126. }
  127.  
  128. void CMainWindow::DoGradientFill (CDC* pDC, CRect* pRect)
  129. {
  130.     CBrush* pBrush[64];
  131.     for (int i=0; i<64; i++) {
  132.         switch (m_nFillColor) {
  133.  
  134.         case 0:
  135.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0, 0));
  136.             break;
  137.  
  138.         case 1:
  139.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4), 0));
  140.             break;
  141.  
  142.         case 2:
  143.             pBrush[i] = new CBrush (RGB (0, 0, 255 - (i * 4)));
  144.             break;
  145.  
  146.         case 3:
  147.             pBrush[i] = new CBrush (RGB (255 - (i * 4), 0,
  148.                 255 - (i * 4)));
  149.             break;
  150.  
  151.         case 4:
  152.             pBrush[i] = new CBrush (RGB (0, 255 - (i * 4),
  153.                 255 - (i * 4)));
  154.             break;
  155.         }
  156.     }
  157.  
  158.     int nWidth = pRect->Width ();
  159.     int nHeight = pRect->Height ();
  160.     CRect rect;
  161.  
  162.     for (i=0; i<nHeight; i++) {
  163.         rect.SetRect (0, i, nWidth, i + 1);
  164.         pDC->FillRect (&rect, pBrush[(i * 63) / nHeight]);
  165.     }
  166.  
  167.     for (i=0; i<64; i++)
  168.         delete pBrush[i];
  169. }
  170.  
  171. void CMainWindow::DoDrawText (CDC* pDC, CRect* pRect)
  172. {
  173.     CFont font;
  174.     int nHeight = -((pDC->GetDeviceCaps (LOGPIXELSY) * m_nHeight) / 72);
  175.  
  176.     font.CreateFont (nHeight, 0, 0, 0, m_bBold ? FW_BOLD : FW_NORMAL,
  177.         m_bItalic, 0, 0, DEFAULT_CHARSET, OUT_CHARACTER_PRECIS,
  178.         CLIP_CHARACTER_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH |
  179.         FF_DONTCARE, "Times New Roman");
  180.  
  181.     pDC->SetBkMode (TRANSPARENT);
  182.     pDC->SetTextColor (RGB (255, 255, 255));
  183.  
  184.     CFont* pOldFont = pDC->SelectObject (&font);
  185.     pDC->DrawText (m_text, -1, pRect, DT_SINGLELINE | DT_CENTER |
  186.         DT_VCENTER);
  187.  
  188.     pDC->SelectObject (pOldFont);
  189. }
  190.  
  191. /////////////////////////////////////////////////////////////////////////
  192. // CMyPropertySheet message map and member functions
  193.  
  194. BEGIN_MESSAGE_MAP (CMyPropertySheet, CPropertySheet)
  195.     ON_BN_CLICKED (ID_APPLY_NOW, OnApplyNow)
  196. END_MESSAGE_MAP ()
  197.  
  198. CMyPropertySheet::CMyPropertySheet (CWnd* pParentWnd) :
  199.     CPropertySheet ("Properties", pParentWnd)
  200. {
  201.     AddPage (&m_bkgndPage);
  202.     AddPage (&m_textPage);
  203. }
  204.  
  205. void CMyPropertySheet::OnApplyNow ()
  206. {
  207.     GetActivePage ()->UpdateData (TRUE);
  208.     ((CMainWindow*) AfxGetMainWnd ())->ApplyNow (this);
  209.  
  210.     m_bkgndPage.SetModified (FALSE);
  211.     m_textPage.SetModified (FALSE);
  212. }
  213.  
  214. /////////////////////////////////////////////////////////////////////////
  215. // CBkgndPage message map and member functions
  216.  
  217. BEGIN_MESSAGE_MAP (CBkgndPage, CPropertyPage)
  218.     ON_CONTROL_RANGE (BN_CLICKED, IDC_SOLID, IDC_CYAN, OnChange)
  219. END_MESSAGE_MAP ()
  220.  
  221. void CBkgndPage::OnChange (UINT nID)
  222. {
  223.     SetModified (TRUE);
  224. }
  225.  
  226. void CBkgndPage::DoDataExchange (CDataExchange* pDX)
  227. {
  228.     CPropertyPage::DoDataExchange (pDX);
  229.  
  230.     DDX_Radio (pDX, IDC_SOLID, m_nFillType);
  231.     DDX_Radio (pDX, IDC_RED, m_nFillColor);
  232. }
  233.  
  234. /////////////////////////////////////////////////////////////////////////////
  235. // CTextPage message map and member functions
  236.  
  237. BEGIN_MESSAGE_MAP (CTextPage, CPropertyPage)
  238.     ON_EN_CHANGE (IDC_TEXT, OnChange)
  239.     ON_EN_CHANGE (IDC_HEIGHT, OnChange)
  240.     ON_BN_CLICKED (IDC_BOLD, OnChange)
  241.     ON_BN_CLICKED (IDC_ITALIC, OnChange)
  242. END_MESSAGE_MAP ()
  243.  
  244. void CTextPage::OnChange ()
  245. {
  246.     SetModified (TRUE);
  247. }
  248.  
  249. void CTextPage::DoDataExchange (CDataExchange* pDX)
  250. {
  251.     CPropertyPage::DoDataExchange (pDX);
  252.  
  253.     DDX_Text (pDX, IDC_TEXT, m_text);
  254.     DDX_Text (pDX, IDC_HEIGHT, m_nHeight);
  255.     DDV_MinMaxInt (pDX, m_nHeight, 8, 144);
  256.     DDX_Check (pDX, IDC_BOLD, m_bBold);
  257.     DDX_Check (pDX, IDC_ITALIC, m_bItalic);
  258. }
  259.  
  260. /////////////////////////////////////////////////////////////////////////
  261. // CAboutDialog message map and member functions
  262.  
  263. BEGIN_MESSAGE_MAP (CAboutDialog, CDialog)
  264.     ON_WM_PAINT ()
  265. END_MESSAGE_MAP ()
  266.  
  267. BOOL CAboutDialog::OnInitDialog ()
  268. {
  269.     CDialog::OnInitDialog ();
  270.  
  271.     CStatic* pStatic = (CStatic*) GetDlgItem (IDC_ICONRECT);
  272.     pStatic->GetWindowRect (&m_rect);
  273.     pStatic->DestroyWindow ();
  274.     ScreenToClient (&m_rect);
  275.  
  276.     return TRUE;
  277. }
  278.  
  279. void CAboutDialog::OnPaint ()
  280. {
  281.     CPaintDC dc (this);
  282.     HICON hIcon = (HICON) ::GetClassLong (AfxGetMainWnd ()->m_hWnd,
  283.         GCL_HICON);
  284.  
  285.     if (hIcon != NULL) {
  286.         CDC dcMem;
  287.         dcMem.CreateCompatibleDC (&dc);
  288.  
  289.         CBitmap bitmap;
  290.         bitmap.CreateCompatibleBitmap (&dc, 32, 32);
  291.         CBitmap* pOldBitmap = dcMem.SelectObject (&bitmap);
  292.  
  293.         CBrush brush (::GetSysColor (COLOR_3DFACE));
  294.         dcMem.FillRect (CRect (0, 0, 32, 32), &brush);
  295.         dcMem.DrawIcon (0, 0, hIcon);
  296.  
  297.         dc.StretchBlt (m_rect.left, m_rect.top, m_rect.Width(),
  298.             m_rect.Height (), &dcMem, 0, 0, 32, 32, SRCCOPY);
  299.  
  300.         dcMem.SelectObject (pOldBitmap);
  301.     }
  302. }
  303.